<html>
   
<?php


     if(!isset($_SESSION)){
session_start();
     }
error_reporting (E_ALL & ~E_NOTICE);
// boooooody al3b hena
 function displayStore($row){
     $count=count($row);
     for($i=0;$i < $count;$i++){                 
       $id=$row[$i]['storeID'];
       
echo "<div class=show >";
     if(isset($_SESSION['login']) and $_SESSION['flag_user']==1)
     { 
echo "<a href="."adminControl.php?action=deleteSt&Sid=$id"."><img src='images/fileclose.png' width=20 height=20  /></a>";
     }
echo "<a href="."showProducts.php?action=showProducts&Sid=$id"."><img src='".$row[$i]['stImage']."' width=150 height=150  /></a>";
echo "<br/>";
  echo $row[$i]['storeName'];
  echo "<br>";
  if($row[$i]['votes']>0){
  $current = $row[$i]['total'] / $row[$i]['votes']; 
 Echo "Current Rating: " . round($current, 1) . "<br>"; 
  }
  else {
    Echo "Current Rating: " . round(0, 1) . "<br>";   
      
  }
  
  echo "<form method='post' action='createStoreControler.php?id=$id'>
      <select  required='required'  name='vote'>
         <option selected>1
         <option>2
         <option>3
         <option>4
         <option>5
    </select>
    
<input type='submit' name='rank' value='Rate'/>    </form>
";
// Echo "<a href='rate.php?mode=vote&voted=1&id='>Vote 1</a> | "; 
 
  echo "</div>";
     
}
 }
 
$flag =false;

 if(isset($_POST['submitST'])){
     if(!isset($_SESSION))
session_start();
   if(isset($_SESSION['login']))
{  
    if((isset($_FILES['image'])) and ($_FILES['image']['name']!='')){
        $img="image";
         
       
    }
    else {
        $img=false;
       
    }
     $name= $_POST["storeName"]; 
     $location=$_POST["location"];
     $phone=$_POST["storephone"];
     include 'StoreClass.php';
     ////store fixed 3shan hn5do mn alsession b3d kda
     $us_id=$_SESSION['user_id'];
     
     $st= new Store($us_id, $name, $location, $phone, $img);
     //$st=new Store();
     //$result=$st->getStores();
   // $st->setData(6, $name, $location, $phone, $img);
    //$st->initStore();
    //$st->getStores();
     //$result=$st->getStoreByID(6);
    //displayStore($result);
   $stID =$st->getLastID();
    $_SESSION['store_id']=$stID;
    $id=$_SESSION['store_id'];
   // echo $id;
    header("location:showProducts.php?action=showProducts&Sid=$id");
} 
else
{
    header("Location:login.php");
}
 }
else if(isset($_POST['saveST'])){
     if(!isset($_SESSION))
session_start();
   if(isset($_SESSION['login']))
{  
    if((isset($_FILES['image'])) and ($_FILES['image']['name']!='')){
        $img="image";
         
       
    }
    else {
        $img=false;
       
    }
     $name= $_POST["storeName"]; 
     $location=$_POST["location"];
     $phone=$_POST["storephone"];
     include 'StoreClass.php';
     ////store fixed 3shan hn5do mn alsession b3d kda
     $us_id=$_SESSION['user_id'];
     $st=new Store();
    
   $stID =$st->getID($us_id);
    $_SESSION['store_id']=$stID[0]['storeID'];
    $id=$_SESSION['store_id'];
    try {
            $st->UpdateStore($id, $name, $location, $phone, $img);

    } catch (Exception $ex) {
       echo $ex->getMessage();
    
    }
   // echo $id;
    header("location:showProducts.php?action=showProducts&Sid=$id");
} 
else
{
    header("Location:login.php");
}
}
else if((@$_GET['action'] and $_GET['action']=="stores")  or isset($_POST['rank'])){
     include 'StoreClass.php';
    
     $st=new Store();
     $result=$st->getStores();
   // $st->setData(6, $name, $location, $phone, $img);
    //$st->initStore();
    //$st->getStores();
     $voted = $_POST['vote'];
     $id = $_GET['id'];
    if(isset($_COOKIE[$cookie])) 
 		{ 
 		die("Sorry You have already ranked that site <p>"); 
 		} 
 
 //Otherwise, we set a cooking telling us they have now voted 
 	else 
 		{ 
            
 		$month = 2592000 + time(); 
 		setcookie(Mysite.$id, Voted, $month); 

 		 //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating 
 mysql_query ("UPDATE store SET total = total+'$voted', votes = votes+1 WHERE storeID = '$id'"); 
 		 
 		}
          //   header("Location:rating.php");
  

   $flag=true;
    include './stores.php';
    // $result=$st->getStoreByID(64);
    // displayStore($result);
}

                
else if(@$_GET['action'] and @$_GET['action']=="create")
{
    
    
     if(!isset($_SESSION))
     {
         session_start();
         
     }
   if(isset($_SESSION['login']))
{ 
       
      $q=$_SESSION["store_id"];
if($q==NULL)
{
    include './createStoreForm.php';
}
else{
	echo"you can create only one store for now wait for our updates.";
	}
}
else
    {
    header("Location:login.php");
}   
   
}
else if(@$_GET['action'] and @$_GET['action']=="editST")
{
    
    
     if(!isset($_SESSION))
     {
         session_start();
         
     }
   if(isset($_SESSION['login']))
{  
       include './StoreClass.php';
       $id= $_SESSION['store_id'];
       $st=new Store();
       $stInfo=$st->getStoreByID($id);
      $name= $stInfo[0]['storeName']; 
     $location=$stInfo[0]["address"];
     $phone=$stInfo[0]['stPhone'];
     $image=$stInfo[0]['stImage'];
     $update='store';
    include './UpdateForm.php';
}
else
    {
    header("Location:login.php");
}   
    
}
else if(@$_GET['action'] and @$_GET['action']=="editPRO")
{
    
    
     if(!isset($_SESSION))
     {
         session_start();
         
     }
   if(isset($_SESSION['login']))
{  
       include './productClass.php';
     $pr=new Product();
     $proID=$_GET['Pid'];
     
     $result=   $pr->getProductByID($proID);
     $size=$result[0]['size']; 
     $price=$result[0]['price'];
     $quantity=$result[0]['quantity'];
     $category=$result[0]['category'];
      $image=$result[0]['proImage'];
     $ProductDescrpition="";
      
     $update='product';
    include './UpdateForm.php';
}
else
    {
    header("Location:login.php");
}   
    
}

    
    

    ?>
</html>
